#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import time
import os
import sqlite3
import pytest


@pytest.fixture(scope='session')
def dbconnect():
    conn = sqlite3.connect("funds.db")
    print("数据库连接成功")
    yield conn
    conn.close()
    print("数据库连接已关闭")


@pytest.fixture(scope='function', autouse=True)
def time_count():
    start_time = time.time()
    yield
    cost = time.time() - start_time
    print("耗时{}秒".format(cost))


def test_count(dbconnect):
    cursor = dbconnect.cursor()
    sql = '''select count(1) from fund;'''
    cursor.execute(sql)
    result = cursor.fetchone()[0]
    assert result == 11201


def test_group_by(dbconnect):
    cursor = dbconnect.cursor()
    sql = '''select count(1) from fund where fundType='QDII';'''
    cursor.execute(sql)
    result = cursor.fetchone()[0]
    assert result == 201


if __name__ == '__main__':
    pytest.main(['-s', os.path.basename(__file__), '--html=report.html'])
